package com.hwpt.hwaccount.core.utils;

import android.content.Context;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hwpt.hwaccount.R;
import com.hwpt.hwaccount.core.data.local.entity.UserEntity;
public class AvatarImageUtils {

    public static void loadAvatarImage(Context context, UserEntity user, ImageView imageView) {
        if (isValidAvatarUrl(user.getAvatarUrl())) {
            try {
                Glide.with(context)
                        .load(Uri.parse(user.getAvatarUrl()))
                        .circleCrop()
                        .placeholder(R.drawable.ic_profile)
                        .error(R.drawable.ic_profile)
                        .into(imageView);
            } catch (Exception e) {
                e.printStackTrace();
                imageView.setImageResource(R.drawable.ic_profile);
            }
        } else {
            // URL无效时使用默认头像
            imageView.setImageResource(R.drawable.ic_profile);
        }
    }

    private static boolean isValidAvatarUrl(String url) {
        if (url == null || url.isEmpty()) {
            return false;
        }

        // 检查是否是有效的URI格式
        try {
            Uri uri = Uri.parse(url);
            return uri != null && !uri.toString().isEmpty();
        } catch (Exception e) {
            return false;
        }
    }
}